{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import glob\n",
    "from PIL import Image\n",
    "import xml.etree.ElementTree as ET\n",
    "import os\n",
    "imt_root = '/home/leeyh/youtube-bb/data/youtubebbdevkit2017/youtubebb2017/JPEGImages/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img_paths = []\n",
    "for img_path in glob.glob(os.path.join(imt_root,'*.jpg')):\n",
    "    img_paths.append(img_path)\n",
    "img_paths.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(img_paths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequences_info = []\n",
    "track_ids = []\n",
    "track_id = []\n",
    "no = 0\n",
    "for i in xrange(len(img_paths)-1):\n",
    "    if i%5000 ==0:\n",
    "        print i\n",
    "    d1 = img_paths[i].split('+')\n",
    "    d2 = img_paths[i+1].split('+')\n",
    "    \n",
    "    img_path = img_paths[i]\n",
    "    lab_path = img_path.replace('JPEGImages','Annotations').replace('jpg','xml')\n",
    "    if os.path.exists(lab_path):\n",
    "        in_file = open(lab_path)\n",
    "        tree=ET.parse(in_file)\n",
    "        root = tree.getroot()\n",
    "        for obj in root.iter('object'):\n",
    "            xmlbox = obj.find('bndbox')\n",
    "            b = [float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)]\n",
    "        track_id.append([img_path,b])\n",
    "\n",
    "\n",
    "\n",
    "        if not (d1[0] == d2[0] and d1[1] == d2[1] and d1[2] == d2[2]):\n",
    "            track_ids.append(track_id)\n",
    "            track_id = []\n",
    "        if not (d1[0] == d2[0]):\n",
    "            sequences_info.append(track_ids)\n",
    "            track_ids = []\n",
    "    else:\n",
    "        no = no+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "with open('/home/leeyh/siamv1/youtube_final.txt', 'w') as outfile:\n",
    "    json.dump(sequences_info, outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img_path = sequences_info[10010][0][3][0]\n",
    "b = sequences_info[10010][0][3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Image.open(img_path).crop([b[0],b[2],b[1],b[3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('/home/leeyh/siamv1/ilsvrc_vid.txt', 'r') as outfile:\n",
    "    root = json.load(outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "new = root +sequences_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('/home/leeyh/siamv1/ilsvrc_youtube.txt', 'w') as outfile:\n",
    "    json.dump(new, outfile)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
